package com.lakeworks.processor;

import com.lakeworks.common.domain.channel.ticket.TicketFactory;
import com.lakeworks.common.domain.ticket.TktTicket;
import com.lakeworks.common.domain.ticket.TktTicketFile;
import com.lakeworks.common.enums.ticket.FlowType;
import com.lakeworks.common.enums.ticket.NodeType;
import com.lakeworks.common.enums.ticket.TicketStatus;
import com.lakeworks.common.utils.StringUtils;
import com.lakeworks.domain.OprProduct;
import com.lakeworks.domain.TktProcess;
import com.lakeworks.event.ReceiveTicketEvent;
import com.lakeworks.mapper.OprProductMapper;
import com.lakeworks.mapper.TktTicketMapper;
import com.lakeworks.processor.base.AbstractIssueProcessor;
import lombok.SneakyThrows;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class ReceiveTicketProcessor extends AbstractIssueProcessor<ReceiveTicketEvent> implements ApplicationContextAware{

    private ApplicationContext applicationContext;

    private static Map<String,String> map;

    static {
        map = new HashMap<String,String>();
        map.put("QUANTITY_MISMATCH","quantityMismatch");
        //map.put("MAJOR_RISK","majorRisk");
        map.put("URGENT_ORDER","urgentOrder");
        map.put("EXPRESS_DUTY","relevanceTicket");
        map.put("LOSE_LESS","relevanceTicket");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }


    @SneakyThrows
    @Override
    public TktProcess apply(
            ReceiveTicketEvent event
    ) {
        var process = super.apply(event);
        TicketFactory factory = event.getTicketFactory();
        List<TktTicketFile> files = new ArrayList<>();
        if (!CollectionUtils.isEmpty(factory.getVouchers())) {
            factory.getVouchers().forEach(f -> {
                files.add(TktTicketFile.builder().remark(factory.getComment()).fileUrl(f).build());
            });
        }
        TktTicket build = TktTicket.builder()
                .orderNo(factory.getOrderNo())
                .channelTicketId(factory.getTicketId())
                .status(TicketStatus.AWAITING.name())
                .type(factory.getType())
                .title(factory.getTitle())
                .label(factory.getLabel())
                .priority(factory.getPriority())
                .barcode(factory.getBarcode())
                .version(0)
                .assignee(event.getOperator())
                .tktTicketFiles(files)
                .nodeId(event.getTktRoute().getNodeId())
                .build();
        build.setChannelId(factory.getChannelId());
        build.setCreateBy(event.getOperator());
        build.setModifyBy(event.getOperator());
        String name = map.get(factory.getType());
        if(StringUtils.isNotBlank(name)){
            Class<ReceiveTicketProcessor> receiveTicketProcessors= ReceiveTicketProcessor.class;
            if(name.equals("relevanceTicket")){
                build.setRelevanceId(factory.getRelevanceId());
            }
            Method methods=receiveTicketProcessors.getMethod(name, TktTicket.class);
            build = (TktTicket) methods.invoke(this, build);
        }
        process.setComment("平台:【创建" + FlowType.valueOf(factory.getType()).getName() + "工单】");
        process.setTicket(build);
        process.setRemark(event.getTicketFactory().getComment());
        return process;
    }


    public TktTicket quantityMismatch(TktTicket tktTicket){
        OprProductMapper oprProductMapper = applicationContext.getBean(OprProductMapper.class);
        //tktTicket.setStatus(TicketStatus.WORKING.name());
        tktTicket.setBarcode(oprProductMapper.selectProductByOrderNo(tktTicket.getOrderNo()).stream().map(OprProduct::getBarcode).collect(Collectors.joining(",")));
        return tktTicket;
    }


    /*public TktTicket majorRisk(TktTicket tktTicket){
        tktTicket.setStatus(TicketStatus.WORKING.name());
        tktTicket.setNodeId(1020l);
        return tktTicket;
    }*/

    public TktTicket urgentOrder(TktTicket tktTicket){
        tktTicket.setStatus(TicketStatus.WORKING.name());
        tktTicket.setNodeId(NodeType.URGENT_ORDER_ONE.getValue());
        return tktTicket;
    }

    public TktTicket relevanceTicket(TktTicket tktTicket){
        TktTicketMapper tktTicketMapper = applicationContext.getBean(TktTicketMapper.class);
        OprProductMapper oprProductMapper = applicationContext.getBean(OprProductMapper.class);
        if(tktTicket.getRelevanceId() != null){
            TktTicket ticket = tktTicketMapper.selectTktTicketBychannelId(tktTicket.getChannelId(),tktTicket.getRelevanceId());
            if(ticket != null){
                tktTicket.setRelevanceId(ticket.getId());
                tktTicket.setBarcode(oprProductMapper.selectProductByOrderNo(tktTicket.getOrderNo()).stream().map(OprProduct::getBarcode).collect(Collectors.joining(",")));
                ticket.setStatus(TicketStatus.PENDING.name());
                tktTicketMapper.updateTktTicket(ticket);
            }
        }
        return tktTicket;
    }


   /* @Override
    public TktProcess apply(
            ReceiveTicketEvent event
    ) {
        var process = super.apply(event);
        TicketFactory factory = event.getTicketFactory();
        List<TktTicketFile> files = new ArrayList<>();
        if (!CollectionUtils.isEmpty(factory.getVouchers())) {
            factory.getVouchers().forEach(f -> {
                files.add(TktTicketFile.builder().remark(factory.getComment()).fileUrl(f).build());
            });
        }
        TktTicket build = TktTicket.builder()
                .orderNo(factory.getOrderNo())
                .channelTicketId(factory.getTicketId())
                .status(TicketStatus.AWAITING.name())
                .type(factory.getType())
                .title(factory.getTitle())
                .label(factory.getLabel())
                .priority(factory.getPriority())
                .barcode(factory.getBarcode())
                .version(0)
                .assignee(event.getOperator())
                .tktTicketFiles(files)
                .nodeId(event.getTktRoute().getNodeId())
                .channelTicketId(event.getTicketFactory().getTicketId())
                .build();
        build.setCreateBy(event.getOperator());
        build.setModifyBy(event.getOperator());
        if(factory.getType().equals(FlowType.QUANTITY_MISMATCH.name())){
            build.setStatus(TicketStatus.WORKING.name());
            build.setBarcode(oprProductMapper.selectBarcodeByOrderNo(build.getOrderNo()).stream().map(OprProduct::getBarcode).collect(Collectors.joining(",")));
            /*if(factory.getType().equals(FlowType.MAJOR_RISK.name())){
                build.setNodeId(1020l);
            }
        }
        if(factory.getType().equals(FlowType.URGENT_ORDER.name())){
            build.setStatus(TicketStatus.WORKING.name());
        }
        if(factory.getType().equals(FlowType.EXPRESS_DUTY.name())
                || factory.getType().equals(FlowType.LOSE_LESS.name())){
            TktTicket ticket = tktTicketMapper.selectTktTicketBychannelId(factory.getRelevanceId());
            if(ticket != null){
                //throw new ception("关联id错误");
                build.setRelevanceId(ticket.getId());
                ticket.setStatus(TicketStatus.PENDING.name());
                tktTicketMapper.updateTktTicket(ticket);
            }
        }
        process.setComment("平台:【创建" + FlowType.valueOf(factory.getType()).getName() + "工单】");
        process.setTicket(build);
        process.setRemark(event.getTicketFactory().getComment());
        return process;
    }*/



}
